import { computed } from "vue";
import { useRouter } from "vue-router";

export default function useSearch(query: any,dateArr:any, getGatewayList: any) {
    const router = useRouter();
    // 计算属性
    const dateStr = computed(() => {
        // console.log(query.query.date);
        const d1 = query.date[0];
        const d2 = query.date[1];
        const time1 = d1 && d1.getTime();
        const time2 = d2 && d2.getTime();
        if (time1 && time2) {
            return time1 + '-' + time2;
        } else {
            return "";
        }
    })

    // tab栏切换
    const tabChangeFn = (tab: any) => {
        console.log(tab.props.name);
        query.agreement = tab.props.name;
        // 获取数据
        getGatewayList(query);
    }

    const searchFn = () => {
        if (dateArr != null && dateArr.length == 2) {
            const time1 = dateArr[0].getTime()
            const time2 = dateArr[1].getTime()
            const timeStr = time1 + '-' + time2
            query.date = timeStr
        }

        getGatewayList(query);
    }
    // 清除条件
    const cleartFn = () => {
        console.log(query);

        query.currPage = 1;
        query.pageSize = 5;
        query.eui = "";
        query.online = "all";
        query.agreement = "all";
        query.date = [];
        dateArr=[]
        getGatewayList(query);
    }
    // 刷新
    const refreshFn = () => {
        router.go(0);
    }
    return { dateStr, tabChangeFn, searchFn, cleartFn, refreshFn }
}